﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Core.TemplatesManagement
{
    public class TemplatesManagement
    {
        public static SPFile CreateNewDocumentByTemplate(string newDocumentName, int TemplateId)
        {
            using (SPSite curSite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = curSite.OpenWeb(SPContext.Current.Web.ID))
                {

                    SPList list = web.Lists["Biblioteca Usuario"];
                    // this always uses root folder
                    SPFolder folder = web.Folders["Biblioteca Usuario"];
                    SPFileCollection fcol = folder.Files;

                    // find the template url and open
                    string sTemplate = list.ContentTypes["Memo"].DocumentTemplateUrl;
                    SPFile spf = web.GetFile(sTemplate);
                    byte[] binFile = spf.OpenBinary();
                    // Url for file to be created
                    string destFile = fcol.Folder.Url + "/" + newDocumentName;

                    // create the document and get SPFile/SPItem for 
                    // new document
                    SPFile addedFile = fcol.Add(destFile, binFile, false);
                    addedFile.Update();
                   
                    SPItem newItem = addedFile.Item;
                    newItem["ContentType"] = "Memo";
                    newItem["Title"] = "Aja";
                    newItem.Update();


                    return addedFile;

                    /*
                    SPFile templateDocument = curWeb.Lists[GlobalConstants.LIST_BIBLIOTECA_TEMPLATES].GetItemById(TemplateId).File;
                    byte[] fileContent = templateDocument.OpenBinary();
                    curWeb.AllowUnsafeUpdates = true;
                    SPDocumentLibrary library = curWeb.Lists[GlobalConstants.LIST_BIBLIOTECA_USUARIO] as SPDocumentLibrary;
                    string destUrl = library.RootFolder.Url + "/" + newDocumentName;
                    SPFile destFile = library.RootFolder.Files.Add(destUrl, fileContent, true);
                    SPListItem item = destFile.Item;
                    item["_dlc_DocId"] = "6U7HTXM4CNAA-4-2";
                    item.Update();
                    /*SPListItem item = library.AddItem();
                    item*/
                    //string destUrl = library.DocumentLibrary.AddItem(Url + "/" + newDocumentName);
                    //SPFile newFile = library.Files.Add(newDocumentName, fileContent, true);
                    //newFile.Update();
                   /*  destFile.Update();
                    curWeb.AllowUnsafeUpdates = false;
                    return destFile;*/
                }
            }
            /*
            SPListItem newItem = newFile.Item;
           
            SPListItem newItem = SPContext.Current.Web.Lists[GlobalConstants.LIST_BIBLIOTECA_USUARIO].AddItem();
            newItem[SPBuiltInFieldId.ContentType] = templateDocument.Item[SPBuiltInFieldId.ContentType];
            byte[] fileContent = templateDocument.OpenBinary();
            SPFile file = newItem.Folder.Files.Add(newDocumentName,fileContent);
            newItem.Update();
            SPContext.Current.Web.AllowUnsafeUpdates = false;*/
            /*
            SPFile file = SPContext.Current.Web.Lists[GlobalConstants.LIST_BIBLIOTECA_USUARIO].RootFolder.Files.Add(newDocumentName, templateDocument.OpenBinary());
            SPItem itemFile = file.Item;
            itemFile["Title"] = "Modificado";
            itemFile.Update();
            file.Update();
            SPContext.Current.Web.AllowUnsafeUpdates = false;*/

        }
    }
}
